﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.Xml;
using System.IO;

namespace JKZP
{
    public partial class EditUserForm : Form
    {
        public string username;
        public bool admin;
        public int rowindex;

        public EditUserForm()
        {
            InitializeComponent();
        }

        private void EditUserForm_Load(object sender, EventArgs e)
        {
            try
            {
                textBox_username.Text = username;
                checkBox_admin.Checked = admin;

                XmlDocument doc = new XmlDocument();
                doc.Load(PP.gRoadFile);
                XmlNodeList nodes = doc.SelectNodes("descendant::road");
                if (nodes != null)
                {
                    foreach (XmlNode node in nodes)
                    {
                        string roadid = node.Attributes["roadid"].Value;
                        string user = node.Attributes["user"].Value;
                        string roadname = node.Attributes["roadname"].Value;
                        bool b = user.Contains(username + "-");
                        checkedListBox1.Items.Add(roadid + "-" + roadname, b);
                    }
                }
            }
            catch { }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_username.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，用户名不能为空";
                    return;
                }

                string roadids = "";
                foreach (object o in checkedListBox1.CheckedItems)
                {
                    string roadid = o.ToString().Substring(0, o.ToString().IndexOf("-") + 1);
                    roadids += roadid;
                }

                DataGridView dgv = ((UserForm)(this.Owner)).dataGridView1;
                if (PP.kdws.JKZP_EditUser(textBox_username.Text, checkBox_admin.Checked.ToString(), roadids) == true)
                {
                    dgv.Rows[rowindex].Cells["admin"].Value = checkBox_admin.Checked.ToString();
                    toolStripStatusLabel1.Text = "修改成功";
                    //更新

                    //if (roadids != "")
                    //{
                    //    XmlDocument doc = new XmlDocument();
                    //    doc.Load(PP.gRoadFile);
                    //    XmlNodeList nodes = doc.SelectNodes("descendant::road");
                    //    if (nodes != null)
                    //    {
                    //        foreach (XmlNode node in nodes)
                    //        {
                    //            if (roadids.Contains(node.Attributes["roadid"].Value + "-"))
                    //            {
                    //                if (!node.Attributes["user"].Value.Contains(username + "-"))
                    //                {
                    //                    node.Attributes["user"].Value = (node.Attributes["user"].Value + username + "-");
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (node.Attributes["user"].Value.Contains(username + "-"))
                    //                {
                    //                    node.Attributes["user"].Value = (node.Attributes["user"].Value.Replace(username + "-", ""));
                    //                }
                    //            }
                    //        }
                    //        doc.Save(PP.gRoadFile);
                    //    }
                    //}

                    PP.ReLoad();
                }
                else
                {
                    toolStripStatusLabel1.Text = "修改失败";
                }
            }
            catch { }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
